<?php
class WbPageBasicController extends  DomController {

	public function page( $id ) {

		$item = DomAr::load( $id, 'WbPage');	
		
		$post = DomFgPost::getInstance();
		$galleryImagesQuery = $item->galleryImages->query()->orderBy('t.order');
		
		if( $_GET['deleteContentImage'] && $image = WbPageBasicGalleryImage::load($_GET['deleteContentImage']) ) {
			$image->delete();
		}
		
		$this->loadController('Wb')->_updatePage( $item );
		if( $post->deleteImage ) {
			$item->image->unlink();
			$item->icon->unlink();
			$item->save();
		}
		
		if( $post->save ) {
			
			$item->title = $post->title;
			$item->contents = $post->contents;
			$item->galleryImagesPerPage = (int)$post->galleryImagesPerPage;
			$item->image->addUploaded( $post->contentImage[0], $post->contentImage[1] );
			
			$p = $post->getPost();
			foreach( $galleryImagesQuery->commit() as $i => $image ) {
				$image->order = $p['galleryImages']['order'][$i];
			}
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
		
		
		$tabPanel = array(
			'type' => 'tabPanel',
			'action' => $this->getUrl('site/'.$id),
			'headerTabs' => true,
			'border' => true,
			'frame' => true,
			'action' => $this->getUrl('page/'.$id.'/'.$parentId),
			'items' => array($message),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
		
		// -------------------------------------------
			
		$overallTab = array(
			'title' => 'Üldine',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$this->loadController('Wb')->_getPageItems( $item ),
				array(
					'type'  => 'number',	
					'name' => 'galleryImagesPerPage',
					'value'  => $item->galleryImagesPerPage,
					'title' => 'Pilte ühel lehel',
				),
			),
		);
		$tabPanel['items'][] = &$overallTab;
		
		// -------------------------------------------

		$contentTab = array(
			'title' => 'Sisu',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 3,
				'columnWidths' => array(),
			),
			'items' => array(
				array(
					'type' => 'image',
					'style' => 'width:100px; height:100px',
					'src' => $item->icon->getUrl(),
					'rowspan' => 3,
				),
				array(
					'type'  => 'textField',	
					'name' => 'title',
					'value'  => $item->title,
					'title' => 'Pealkiri',
					'style' => 'width:500px',
				),
				array(
					'type' => 'file',
					'name' => 'contentImage',
					'title' => 'Pilt',
				),
				array(
					'type' => 'value',
				),
				array(
					'type' => 'button',
					'title' => 'Kustuta pilt',
					'name' => 'deleteImage',
					'value' => '1'
				),
				array(
					'type' => 'htmlEditor',
					'value' => $item->contents,
					'name' => 'contents',
					'style' => 'height:400px;',
					'imageManagerUrl' => $this->getUrl('contentImageHandler/'.$id),
					'colspan' => 3,
				),
			),
		);
		$tabPanel['items'][] = &$contentTab;

		// -------------------------------------------

		if( $item->id > 0 ) {
			
			function getIcon( &$row ) {
				return $row->icon->toHtml();
			}

			$galleryTab = array(
				'id' => 'gallery',
				'title' => 'Galerii',
				'layout' => 'table',
				'layoutConfig' => array(
					'columns' => 2,
					'columnWidths' => array(150,),
				),
				'items' => array(
					array(
						'type' => 'upload',
						'handlerUrl' => $this->getUrl('addGalleryImage/'.$item->id.'/'),
						'refreshUrl' => $this->getUrl('page/'.$item->id.'/&tab=gallery'),
						'fileTypes' => '*.png;*.jpg;*.gif;',
						'name' => 'image',
						'title' => 'Pildid',
					),
					array(
						'type' => 'imageGrid',
						'id' => 'galleryImages',
						'name' => 'galleryImages',
						'colspan' => 2,
						'ordering' => true,
						'link' => $this->getUrl('simpleGrid'),
						'source' => new DomArDataSource( $galleryImagesQuery, 99999),
						'fields' => array(
							array('handler'=>'getIcon'),
							array('type' => 'gridButton','value' => 'Delete','link' => $this->getUrl('page/'.$id.'/'.$parentId.'/&tab=gallery&deleteContentImage='),'confirmMessage' => 'Olete kindel, et soovite pildi kustutada?',),
						),
					),
				),
			);
			$tabPanel['items'][] = &$galleryTab;
		}
		// -------------------------------------------
		
		$tabPanel['items'][] = $this->loadController('Wb')->_getPagePanels( $item );
		
		// -------------------------------------------
		$this->view->contents = DomFg::render( $tabPanel );
		$this->loadController('Wb')->_renderSiteTree( $item->getRoot(), $item );
		
	}

	public function contentImageHandler( $pageId ) {
		
		$this->view->setTemplate('popup.html');
		
		$page = DomAr::load( $pageId, 'WbPage' );
		
		if( $_GET['delete'] && $item = WbPageBasicContentImage::load( $_GET['delete'] )){
			$item->delete();
		}
		
		$post = DomFgPost::getInstance();
		if( $post->uploadImage ) {
			$image = new WbPageBasicContentImage();
			$image->image->addUploaded( $post->newImage[0], $post->newImage[1] );
			$image->page = $page;
			if( $image->save() ) {
				$message = array('type' => 'ok','colspan' => 3,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' =>3,'description' => implode('<br>',$image->getValidationReport()->messages),);
			}
		}
							
		function addHandler( $row ) {
			return '<a href="javascript:updateHtmlEditorImage('.$row->id.',\''.$row->image->getUrl().'\');">Lisa teksti</a>';
		}
		
		function imageHandler( $row ) {
			return '<a href="javascript:updateHtmlEditorImage('.$row->id.',\''.$row->image->getUrl().'\');">'.$row->icon->toHtml().'</a>';
		}

		$panel = array(
			'title' => 'Images',
			'layout' => 'table',
			'action' => $this->getUrl('contentImageHandler/'.$pageId),
			'layoutConfig' => array(
				'columns' =>3,
				'columnWidths' => array(150),
			),
			'items' => array(
				$message,
				array(
					'title' => 'Lisa uus pilt',
					'type' => 'file',
					'name' => 'newImage',
				),
				array(
					'type' => 'button',
					'title' => 'Lae üles',
					'name' => 'uploadImage',
				),
				array(
					'type' => 'imageGrid',
					'colspan' => 3,
					'source' => new DomArDataSource( $page->contentImages->query()->orderBy('t.id DESC'), 40 ),
					'fields' => array(
						array('handler'=>'imageHandler'),
						array('handler'=>'addHandler'),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('contentImageHandler/'.$pageId.'&delete='), 'confirmMessage' => 'Olete kindel, et soovite pildi kustutada?' ),
					),
				),
			),
		);
		
		$this->view->contents = DomFg::render( $panel );
	
	}
	
	public function addGalleryImage( $pageId ) {

		$this->view->setTemplate(false);
		echo '1';
		
		$page = DomAr::load( $pageId, 'WbPageBasic');
		if( !$page ) {
			exit(0);
		}
		$im = new WbPageBasicGalleryImage();
		$im->page = $page;
		$im->image->addUploaded( $_FILES['Filedata']['tmp_name'], $_FILES['Filedata']['name']);
		$im->save();

		exit(0);
	}
	

}